package org.myontology.controller.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import model.RankedElement;

import org.hibernate.classic.Session;
import org.myontology.controller.AbstractAction;
import org.myontology.controller.AttributeHolder;
import org.myontology.controller.AttributeLevel;
import org.myontology.controller.ValueHandler;
import org.openrdf.model.URI;

public class CloudAction extends AbstractAction {

	private Session session;
	
	
	
	public CloudAction() {
		
	}

	public static final ValueHandler<List<RankedElement>> CLASSES = new ValueHandler<List<RankedElement>>(AttributeLevel.REQUEST,CloudAction.class);
	public static final ValueHandler<List<RankedElement>> OMS = new ValueHandler<List<RankedElement>>(AttributeLevel.REQUEST,CloudAction.class);
	public static final ValueHandler<List<RankedElement>> PROPS = new ValueHandler<List<RankedElement>>(AttributeLevel.REQUEST,CloudAction.class);
	
	public void execute(AttributeHolder attributes) throws Exception {
		
		
		

		Statement stmt = session.connection().createStatement();
		
		loadRankedElements(attributes,  "Class", CLASSES, stmt);
		loadRankedElements(attributes,  "Property", PROPS, stmt);
		loadRankedElements(attributes,  "OntologyModule", OMS, stmt);
	}

	/**
	 * @param attributes
	 * @param cal
	 * @param type
	 * @param handler
	 * @param stmt
	 * @throws SQLException
	 */
	private static void loadRankedElements(AttributeHolder attributes, 
			String type, ValueHandler<List<RankedElement>> handler,
			Statement stmt) throws SQLException {
		List<RankedElement> classes = loadRankedElements(type, stmt);
		handler.setValue(attributes, classes);
	}

	/**
	 * @param type
	 * @param stmt
	 * @return
	 * @throws SQLException
	 */
	private static List<RankedElement> loadRankedElements(String type,
			Statement stmt) throws SQLException {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.HOUR,-1*24*7);
		String query = "SELECT LIMIT 0 100 name,type,COUNT(*) AS c FROM logs WHERE type='"+type+"' AND action=2 AND time > '"+new Timestamp(cal.getTimeInMillis())+"' GROUP BY name,type ORDER  BY c DESC";
		ResultSet rs = stmt.executeQuery(query);
		List<RankedElement> classes = new ArrayList<RankedElement>();
		while (rs.next()) {
			RankedElement re = new RankedElement(rs.getString(1),rs.getString(2));
			re.setValue(rs.getInt(3));
			classes.add(re);
		}
		
		for (Iterator<RankedElement> it = classes.iterator();it.hasNext();) {
			if (it.next().getLabel()==null)
				it.remove();
		}
		
		Collections.sort(classes, new Comparator<RankedElement>() {

			public int compare(RankedElement o1, RankedElement o2) {
				if (o1==null)
					return -1;
				if (o2==null)
					return 1;
				return o1.getLabel().compareTo(o2.getLabel());
			}
			
		});
		return classes;
	}

	public static List<URI> loadRankedURIs (String type,
			Statement stmt) throws SQLException {
		List<RankedElement> list = loadRankedElements(type, stmt);
		List<URI> results = new ArrayList<URI>();
		
		for (Iterator<RankedElement> it = list.iterator();it.hasNext();) {
			RankedElement r = it.next();
			results.add(r.getUri());
		}
		
		return results;
	}
	
	
}
